package com.jchao.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jchao.auth.mapper.SysUserMapper;
import com.jchao.auth.service.SysMenuService;
import com.jchao.auth.service.SysUserService;
import com.jchao.model.system.SysUser;
import com.jchao.security.custom.LoginUserInfoHelper;
import com.jchao.vo.system.LoginVo;
import com.jchao.vo.system.RouterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * create 2023/11/14 0:51
 */


@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysMenuService menuService;

    @Autowired
    private SysUserMapper  sysUserMapper;

    @Override
    public void updateStatus(Long id, Integer status) {
        LambdaUpdateWrapper<SysUser> wrapper = new LambdaUpdateWrapper();
        wrapper.eq(SysUser::getId,id)
                .set(SysUser::getStatus,status);
        this.update(wrapper);
    }

    @Override
    public SysUser getByUserName(String userName) {
      return  this.getOne(new LambdaUpdateWrapper<SysUser>().eq(SysUser::getUsername, userName));
    }

    @Override
    public Map<String, Object> getUserInfo(String username) {

        Map<String, Object> result = new HashMap<>();
        SysUser sysUser = this.getByUserName(username);

        //根据用户id获取菜单权限值
        List<RouterVo> routerVoList = menuService.findUserMenuList(sysUser.getId());
        //根据用户id获取用户按钮权限
        List<String> permsList = menuService.findUserPermsList(sysUser.getId());

        result.put("name", sysUser.getName());
        result.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        //当前权限控制使用不到，我们暂时忽略
        result.put("roles",  new HashSet<>());
        result.put("buttons", permsList);
        result.put("routers", routerVoList);
        return result;
    }

    @Override
    public SysUser getByUsername(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername,username);
        SysUser sysUser = baseMapper.selectOne(wrapper);
        return sysUser;
    }

    @Override
    public SysUser getUserByUserName(String assignee) {
       return this.getOne(new LambdaUpdateWrapper<SysUser>().eq(SysUser::getUsername, assignee));
    }

    @Override
    public Map<String, Object> getCurrentUser() {
        SysUser sysUser = sysUserMapper.selectById(LoginUserInfoHelper.getUserId());
        //SysDept sysDept = sysDeptService.getById(sysUser.getDeptId());
        //SysPost sysPost = sysPostService.getById(sysUser.getPostId());
        Map<String, Object> map = new HashMap<>();
        map.put("name", sysUser.getName());
        map.put("phone", sysUser.getPhone());
        //map.put("deptName", sysDept.getName());
        //map.put("postName", sysPost.getName());
        return map;

    }
}
